//
//  UCLine.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2016/11/15.
//  Copyright © 2016年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "UCPolyline.h"
#import <GoogleMaps/GoogleMaps.h>
#import "UCMapView.h"
#import "UCMarker.h"
#import "Public.h"
#import "UCMutablePath.h"
#import "UCGeometryUtils.h"

@interface UCStrokeStyle()
@property(nonatomic,strong,readwrite)GMSStrokeStyle *strokeStyle;
@end
@implementation UCStrokeStyle

+ (instancetype)solidColor:(UIColor *)color{
    return [[UCStrokeStyle alloc] initSolidColor:color];
}

- (instancetype)initSolidColor:(UIColor *)color{

    self = [super init];
    if (self) {
        self.strokeStyle = [GMSStrokeStyle solidColor:color];
    }
    return self;
}

+ (instancetype)gradientFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor{
    return [[UCStrokeStyle alloc] initGradientFromColor:fromColor toColor:toColor];
}
- (instancetype)initGradientFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor{
    self = [super init];
    if (self) {
        self.strokeStyle = [GMSStrokeStyle gradientFromColor:fromColor toColor:toColor];
    }
    return self;
}
@end


@interface UCStyleSpan()
@property(nonatomic,strong)GMSStyleSpan *styleSpan;
@property(nonatomic, readwrite) UCStrokeStyle *style;
@property(nonatomic, readwrite) double segments;
@end

@implementation UCStyleSpan

+ (instancetype)spanWithColor:(UIColor *)color{
    return [[UCStyleSpan alloc] initSpanWithColor:color];
}
- (instancetype)initSpanWithColor:(UIColor *)color{
    self = [super init];
    if (self) {
        self.styleSpan = [GMSStyleSpan spanWithColor:color];
    }
    return self;
}

+ (instancetype)spanWithColor:(UIColor *)color segments:(double)segments{
    return [[UCStyleSpan alloc] initSpanWithColor:color segments:segments];
}
- (instancetype)initSpanWithColor:(UIColor *)color segments:(double)segments{
    self = [super init];
    if (self) {
        self.styleSpan = [GMSStyleSpan spanWithColor:color segments:segments];
    }
    return self;
}

+ (instancetype)spanWithStyle:(UCStrokeStyle *)style{
    return [[UCStyleSpan alloc] initSpanWithStyle:style];
}
- (instancetype)initSpanWithStyle:(UCStrokeStyle *)style{
    self = [super init];
    if (self) {
        self.style = style;
        self.styleSpan = [GMSStyleSpan spanWithStyle:style.strokeStyle];
    }
    return self;
}

+ (instancetype)spanWithStyle:(UCStrokeStyle *)style segments:(double)segments{
    
    return [[UCStyleSpan alloc] initSpanWithStyle:style segments:segments];
}

- (instancetype)initSpanWithStyle:(UCStrokeStyle *)style segments:(double)segments{
    self = [super init];
    if (self) {
        self.style = style;
        self.segments = segments;
        self.styleSpan = [GMSStyleSpan spanWithStyle:style.strokeStyle segments:segments];
    }
    return self;
}

- (UCStrokeStyle *)style{

    return _style;
}

- (double)segments{
    return _segments;
}
@end



@interface UCPolyline()
{
//    int _zIndex;
//    UCMapView *_mapView;
}
@property(nonatomic,strong)GMSPolyline *polyline;
@property(nonatomic,strong)NSMutableArray *markers;

@end

@implementation UCPolyline

+ (instancetype)polylineWithPath:(UCPath *)path{
    return [[UCPolyline alloc] initPolylineWithPath:path];
}
- (instancetype)initPolylineWithPath:(UCPath *)path{
    self = [super init];
    if (self) {
        self.polyline = [GMSPolyline polylineWithPath:[path valueForKey:@"gmPath"]];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.polyline = [GMSPolyline new];
    }
    return self;
}

//+ (instancetype)new{
//    UCPolyline *line = [super new];
//    line.polyline = [GMSPolyline new];
//    return line;
//}

- (void)setMapView:(UCMapView *)mapView{
    super.mapView = mapView;
    self.polyline.map = [mapView valueForKey:@"map"];
    
    if (mapView == nil && _showLabel) {
        
        [self removeMarkerFromMap];
    }
}

- (void)setGeodesic:(BOOL)geodesic{
    self.polyline.geodesic = geodesic;
}
- (BOOL)geodesic{
    return self.polyline.geodesic;
}

- (void)setStrokeColor:(UIColor *)strokeColor{
    self.polyline.strokeColor = strokeColor;
}
- (UIColor *)strokeColor{
    return self.polyline.strokeColor;
}

- (void)setStrokeWidth:(CGFloat)strokeWidth{
    self.polyline.strokeWidth = strokeWidth;
}
- (CGFloat)strokeWidth{
    return self.polyline.strokeWidth;
}

- (void)setPath:(UCPath *)path{

    self.polyline.path = [path valueForKey:@"gmPath"];
    
    if (self.showLabel) {
        [self addMarker];
    }
}

- (void)setZIndex:(int)zIndex{
    super.zIndex = zIndex;
    self.polyline.zIndex = zIndex;
}

- (void)setSpans:(NSArray<UCStyleSpan *> *)spans{
    _spans = spans;
    NSMutableArray *array = [NSMutableArray array];
    for (UCStyleSpan *span in spans) {
        [array addObject:span.styleSpan];
    }
    self.polyline.spans = array;
}

- (void)closeEiditPoline{
    for (GMSMarker *marker in self.markers) {
        marker.map = nil;
    }
    [self.markers removeAllObjects];
    
}

- (void)compilePolyline{

    NSInteger count = _path.count;
    for (int i = 0; i< count; i++) {
        CLLocationCoordinate2D position = [_path coordinateAtIndex:i];
        GMSMarker *marker = [GMSMarker markerWithPosition:position];
        marker.draggable = YES;
        marker.groundAnchor = CGPointMake(0.5, 0.5);
        marker.icon = MYBUNDLE_IMAGE_PATH(@"圆圈");
        marker.map = [super.mapView valueForKey:@"map"];
        [self.markers addObject:marker];
    }
}

- (UCPath *)compilePolylineWithMarker:(UCMarker *)marker{
    if (self.markers.count) {
        for (int i = 0; i<self.markers.count; i++) {
            if ([marker valueForKey:@"marker"] == self.markers[i]) {
                [(GMSMutablePath *)self.path replaceCoordinateAtIndex:i withCoordinate:marker.coordinate];
            }
        }
    }
    return self.path;
}

- (NSMutableArray *)markers{
    if (!_markers) {
        _markers = [NSMutableArray array];
    }
    return _markers;
}


- (void)removeMarkerFromMap{
    for (GMSMarker *marker in _markers) {
        marker.map = nil;
    }
    [_markers removeAllObjects];
}

- (void)addMarker{

    NSInteger sumCount = self.path.count;
    //撤销操作
    if (_markers.count == sumCount) {
        GMSMarker *marker = [_markers lastObject];
        marker.map = nil;
        [_markers removeLastObject];
        return ;
    }
    
    if (sumCount > 1) {
        UCMutablePath *centerPath = [UCMutablePath path];
        CLLocationCoordinate2D coord2 = [self.path coordinateAtIndex:sumCount -1];
        CLLocationCoordinate2D coord1 = [self.path coordinateAtIndex:sumCount -2];
        [centerPath addCoordinate:coord1];
        [centerPath addCoordinate:coord2];
        
        CLLocationDegrees latitude = fabs((coord1.latitude - coord2.latitude)/2 + coord2.latitude);
        CLLocationDegrees longitude = fabs((coord1.longitude - coord2.longitude)/2 + coord2.longitude);
        CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(latitude, longitude);
        
        NSString *content = [NSString stringWithFormat:@"%.2f米",CalculateTheDistanceOfTheLine(centerPath)];
        
        GMSMarker *marker = [GMSMarker markerWithPosition:centerLocation];
        
        if ([self.delegate respondsToSelector:@selector(showPolineMiddleLabelWithLabelContent:)]) {
            marker.icon = [self.delegate showPolineMiddleLabelWithLabelContent:content];
        }else{
            
            marker.icon = [self private_setIconImage:@{
                                                       NSFontAttributeName : [UIFont systemFontOfSize:13],
                                                       NSForegroundColorAttributeName : [UIColor whiteColor],
                                                       NSBackgroundColorAttributeName : [UIColor blackColor]
                                                       }
                                          withString:content];
        }
        marker.zIndex = self.zIndex;
        marker.map = [super.mapView valueForKey:@"map"];
        [self.markers addObject:marker];
    }
}


- (UIImage *)private_setIconImage:(NSDictionary *)dic withString:(NSString *)string{
    
    CGSize size = [string sizeWithAttributes:dic];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    
    //2、设置裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3];
    [path addClip];
    
    [string drawInRect:rect withAttributes:dic];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end
